package com.ecp.commons.bean;

import java.util.ArrayList;
import java.util.List;

import com.ecp.commons.annotation.VariantType;
import com.ecp.commons.serialization.GenericBean;
import com.ecp.commons.serialization.Variant;

public class Region extends GenericBean {

	@Override
	public void defineVariantSet() {
		this.entity = "region";
		map.put("id", new Variant().setType(VariantType.INTEGER).setPk(true).setNullable(false));
		map.put("name", new Variant().setLength(64));
		map.put("parentId", new Variant().setType(VariantType.INTEGER).setNullable(false));
		map.put("description", new Variant().setLength(255));
		map.put("displayOrder", new Variant().setType(VariantType.INTEGER).setNullable(false));
		map.put("path", new Variant().setLength(255));
	}

	@Override
	public void resetValues() {
		this.set("id", 0);
		this.set("name", "");
		this.set("parentId", 0);
		this.set("description", "");
		this.set("displayOrder", 0);
		this.set("path", "");
	}

	public int getId() {
		return this.getInt("id");
	}

	public String getName() {
		return this.getString("name");
	}

	public int getParentId() {
		return this.getInt("parentId");
	}

	public String getDescription() {
		return this.getString("description");
	}

	public int getDisplayOrder() {
		return this.getInt("displayOrder");
	}

	public String getPath() {
		return this.getString("path");
	}

	public List<Integer> getPathIds() {
		List<Integer> pathIds = new ArrayList<Integer>();
		String path = this.getString("path");
		String[] ids = path.split("/");
		ids[0] = "0"; // 分割后第一个是空字串，直接赋值0，也好用于追溯和转换成int数组。
		for (String id : ids) {
			pathIds.add(Integer.valueOf(id));
		}
		return pathIds;
	}

	public void setId(int id) {
		this.set("id", id);
	}

	public void setName(String name) {
		this.set("name", name);
	}

	public void setParentId(int parentId) {
		this.set("parentId", parentId);
	}

	public void setDescription(String description) {
		this.set("description", description);
	}

	public void setDisplayOrder(int displayOrder) {
		this.set("displayOrder", displayOrder);
	}

	public void setPath(String path) {
		this.set("path", path);
	}
	
	public boolean isLeaf() {
		String path = this.getString("path");
		return (path.split("/").length > 4);
	}
}
